package com.battlebardgames.simplejavacache.key;

import java.lang.reflect.Method;

import com.battlebardgames.simplejavacache.CacheEngine;

/**
 * Key provider which provides a unique key per object instance
 * @author Thomas Cashman
 */
public class PerInstanceKeyProvider implements ICacheKeyProvider {

	@Override
	public String getCacheKey(Object object, Method method, Object[] parameters) {
		String result = CacheEngine.CACHE_KEY_HEADER + 
				object.getClass().getCanonicalName() + "_" +
				object.hashCode() + "_" + method.getName();
		if(parameters != null && parameters.length > 0) {
			for(int i = 0; i < parameters.length; i++) {
				if(parameters[i] == null)
					result += "_" + parameters[i].toString();
				else
					result += "_null";
			}
		}
		return result;
	}

	@Override
	public String getCacheKey(Object object) {
		String result = CacheEngine.CACHE_KEY_HEADER + 
				object.getClass().getCanonicalName() + "_" +
				object.hashCode();
		return result;
	}

}
